/**
 * @author Eduardo Leonne Souza de Oliveira
 * @version 1.0
 * Date: 2011-12-29
 * Last change: 2011-12-29 
 * 
 */

package com.leonne.harpacristabrasileira.tools;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import android.content.Context;

/**
 * This class is an adaptation from an internet page.
 * <p> Unfortunately I can't find the page again to give the credits here.
 */
public class ReadFile {
	
	private InputStream inputStream;
	
	/**
	 * Constructor that load the file from the resources
	 * 
	 * @param context The base context of the application
	 * @param file The file name that will be opened. 
	 * <p>The file needs to stay in the assets folder. 
	 * <p>With the file in this folder, just needs to pass the file name, without any other path.
	 */
	
	public ReadFile(Context context, String file) {
		try {
			this.inputStream = context.getAssets().open(file);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * Gets the content of the file
	 * 
	 * @return String Returns the content of the file opened.
	 */
	
	public String getString() {
	    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

	    byte buf[] = new byte[1024];
	    int len;
	    try {
	        while ((len = this.inputStream.read(buf)) != -1) {
	            outputStream.write(buf, 0, len);
	        }
	        outputStream.close();
	        this.inputStream.close();
	    } catch (IOException e) {
	    	e.printStackTrace();
	    }
	    return outputStream.toString();
	}

}
